package com.example.administrator.xxnapp.friend;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.administrator.xxnapp.R;
import com.example.administrator.xxnapp.bean.UserBean;

import java.util.List;

/**
 * Created by Administrator on 2018/5/13.
 */

public class RVFriendAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<UserBean> mDatas;

    public RVFriendAdapter(Context context, List<UserBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new RVFriendViewHolder(LayoutInflater.from(context).inflate(R.layout.item_friend_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RVFriendViewHolder){
            ((RVFriendViewHolder) holder).bindView(position);
        }
    }

    @Override
    public int getItemCount() {
        return mDatas == null?0:mDatas.size();
    }

    private class RVFriendViewHolder extends RecyclerView.ViewHolder{
        private View mViewRoot;
        private ImageView mIVHead;
        private TextView mTVName,mTVPhoneNum;

        public RVFriendViewHolder(View itemView) {
            super(itemView);
            mViewRoot = itemView.findViewById(R.id.item_friend_layout_Root);
            mIVHead = itemView.findViewById(R.id.item_friend_layout_IV_Head);
            mTVName = itemView.findViewById(R.id.item_friend_layout_TV_UserName);
            mTVPhoneNum = itemView.findViewById(R.id.item_friend_layout_TV_PhoneNum);
        }

        private void bindView(final int position){
            UserBean bean = mDatas.get(position);
            RequestOptions options = new RequestOptions();
            options.circleCrop();
            options.error(R.mipmap.ic_launcher_round);
            Glide.with(context).load(bean.getHeadImage()).apply(options).into(mIVHead);

            mTVName.setText(bean.getUserName());
            mTVPhoneNum.setText(bean.getUserPhone());

            mViewRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onFriendItemClickListener != null){
                        onFriendItemClickListener.onItemClick(position);
                    }
                }
            });
        }
    }

    private OnFriendItemClickListener onFriendItemClickListener;

    public void setOnFriendItemClickListener(OnFriendItemClickListener onFriendItemClickListener) {
        this.onFriendItemClickListener = onFriendItemClickListener;
    }

    public static abstract class OnFriendItemClickListener{
        abstract void onItemClick(int position);
    }
}
